﻿/*
 *  Copyright 2009 Coditate Software
 *  Licensed under the GNU Library General Public License (LGPL) 2.1 
 *  
 *  License available at: http://simplesavant.codeplex.com/license
 */
using System;

namespace Coditate.Savant.Async
{
    /// <summary>
    /// Tracks the result of an asynchronous call to Savant via extension methods (<see cref="AsyncExtensions"/>).
    /// </summary>
    /// <remarks>
    /// This class tracks the results of asynchronous actions. These "actions" are used 
    /// with any method that has no return value (void return).
    /// </remarks>
    public class AsyncAction : AsyncOperation
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncAction"/> class.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="savant">The savant instance.</param>
        internal AsyncAction(Action<ISimpleSavant> action, ISimpleSavant savant) : base(savant)
        {
            Action = action;
        }

        /// <summary>
        /// Gets or sets the action to invoke.
        /// </summary>
        /// <value>The action.</value>
        internal Action<ISimpleSavant> Action { get; private set; }

        /// <summary>
        /// Ends the asynchronous invocation.
        /// </summary>
        /// <returns></returns>
        /// <remarks>
        /// This method blocks until the action completes.  If the action invocation
        /// threw an exception it is rethrown here.
        /// </remarks>
        public void EndInvoke()
        {
            Action.EndInvoke(Result);
        }

        internal void BeginInvoke()
        {
            Result = Action.BeginInvoke(Savant, null, null);
        }
    }
}